/**!
 *
 * 添加用户。
 *
 */

$(function () {
    /** 用户表单的 jQuery 对象 */
    var $userForm = $("#user_form");
    /** 账号远程正在校验的图标的 jQuery 对象 */
    var $loginidIconLoading = $("#loginid_icon_loading");
    /** 账号远程校验通过的图标的 jQuery 对象 */
    var $loginidIconSuccess = $("#loginid_icon_success");
    /** 账号远程校验未通过的图标的 jQuery 对象 */
    var $loginidIconError = $("#loginid_icon_error");
    /** 显示校验信息的容器 */
    var $validatecontainer = $("#validate_container");
    /** 添加按钮的 jQuery 对象 */
    var $addUserBtn = $userForm.find("button[name='addUserBtn']");

    $.validator.addMethod("checkLoginid", function (value, element) {
        return this.optional(element) || usernameRE.test(value);
    });

    $userForm.validate({
        rules: {
            loginid: {
                required: true,
                minlength: 5,
                maxlength: 20,
                checkLoginid: true,
                remote: {
                    url: "{0}web/user/check/loginid.json".format(globalBasePath),
                    beforeSend: function () {
                        $loginidIconLoading.removeClass("hide");
                        $loginidIconSuccess.addClass("hide");
                        $loginidIconError.addClass("hide")
                    },
                    dataFilter: function (data, type) {
                        $loginidIconLoading.addClass("hide");

                        data = $.parseJSON(data);

                        // 传给服务器的账号未存在
                        if (data.resultCode == 0) {
                            $loginidIconSuccess.removeClass("hide");

                            return true;
                        }

                        $loginidIconError.removeClass("hide");

                        // 传给服务器的账号已存在
                        return false;
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        $loginidIconLoading.addClass("hide");
                        $loginidIconError.removeClass("hide");

                        // 获取状态码
                        var statusCode = xhr.status;
                        // 服务器返回的字符串
                        var responseText = xhr.responseText;

                        // 登录超时
                        if (statusCode == 401) {
                            $.toast({
                                top: 5,
                                msg: "您已登录超时，请重新登录",
                                type: "error",
                                close: 6000
                            });

                            // 无访问权限
                        } else if (statusCode == 403) {
                            $.toast({
                                top: 5,
                                msg: "您无访问权限，请联系管理授权",
                                type: "error",
                                close: 6000
                            });

                            // 请求参数不合法
                        } else if (statusCode == 412) {
                            $.toast({
                                top: 5,
                                msg: "您的请求参数不合法--->" + responseText,
                                type: "error",
                                close: 6000
                            });

                            // 访问的页面不存在
                        } else if (statusCode == 404) {
                            $.toast({
                                top: 5,
                                msg: "您访问的页面不存在",
                                type: "error",
                                close: 6000
                            });

                            // 服务器内部错误
                        } else if (statusCode == 500) {
                            $.toast({
                                top: 5,
                                msg: "系统繁忙，请稍候再试",
                                type: "error",
                                close: 6000
                            });

                            // 其他
                        } else {
                            $.toast({
                                top: 5,
                                msg: "网络连接失败，请稍候再试",
                                type: "error",
                                close: 6000
                            });
                        }
                    }
                }
            },
            loginName: {
                required: true,
                maxlength: 50,
            },
            collieryNo: {
                required: false,
                maxlength: 20
            },
            industryCode: {
                required: true
            },
            isAdmin: {
                required: true
            },
            isLeader: {
                required: true
            }
        },
        messages: {
            loginid: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入账号</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>账号的长度不能大于{0}个字符</span>",
                checkLoginid: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>账号由数字、字母、下划线组成</span>",
                remote: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>账号已存在，请重新输入</span>"
            },
            loginName: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入姓名</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>姓名的长度不能大于{0}个字</span>"
            },
            collieryNo: {
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>企业编码的长度不能大于{0}个字符</span>"
            },
            industryCode: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请选择所属行业</span>"
            },
            isAdmin: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请选择用户类型</span>"
            },
            isLeader: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请选择是否为领导</span>"
            }
        },
        errorContainer: $validatecontainer,
        errorLabelContainer: $("ul", $validatecontainer),
        wrapper: "li",
        submitHandler: function (form) {
            $addUserBtn.html("<i class='ace-icon fa fa-spinner fa-spin bigger-150'></i>正在提交…").attr({"disabled": "disabled"});

            return true;
        }
    });
});